package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.ContractProductExample;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.service.cargo.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public void save(ContractProduct contractProduct) {
        //修改购销合同的数量
        //修改总金额 原金额+当前货物的小计

        //计算当前货物的小计金额
        Double amount=contractProduct.getCnumber()*contractProduct.getPrice();
        contractProduct.setAmount(amount);
        //查询购销合同
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setProNum(contract.getProNum()+1);
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        contractDao.updateByPrimaryKeySelective(contract);


        contractProductDao.insertSelective(contractProduct);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //更新总金额  :   总金额-旧的小计金额+新的小计金额
        //新的小计 此处记得设置新的小计
        Double newAmount=contractProduct.getCnumber()*contractProduct.getPrice();
        contractProduct.setAmount(newAmount);
        //查询旧的小计金额
        ContractProduct contractProduct_old = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        Double oldAmount = contractProduct_old.getAmount();
        //查询总金额
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        Double totalAmount = contract.getTotalAmount();

        contract.setTotalAmount(totalAmount-oldAmount+newAmount);
        //计算完成之后记得将contract更新
        contractDao.updateByPrimaryKeySelective(contract);
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    public void delete(String id) {
        //合同货物数量,附件数量,金额
        //根据id查找货物
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //查找该货物下的附件  此处货物与附件的关系是一对多  映射文件已经写了所以不用自己写
        List<ExtCproduct> extCproductList = contractProduct.getExtCproducts();
        Double totalExtAmount=0.0;
        for (ExtCproduct extCproduct : extCproductList) {
            totalExtAmount+=extCproduct.getAmount();
            //删除附件
            extCproductDao.deleteByContractId(extCproduct.getId());
        }

        String contractId = contractProduct.getContractId();
        //根据id找到合同
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        //货物数量
        contract.setProNum(contract.getProNum()-1);
        //附件数量
        contract.setExtNum(contract.getExtNum()-extCproductList.size());
        //总金额
        contract.setTotalAmount(contract.getTotalAmount()-contractProduct.getAmount()-totalExtAmount);
        //更新合同
        contractDao.updateByPrimaryKeySelective(contract);
        contractProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> contractProductList = contractProductDao.selectByExample(example);
        return new PageInfo(contractProductList,5);
    }

    @Override
    public void saveList(List<ContractProduct> cpList) {
        for (ContractProduct contractProduct : cpList) {
            this.save(contractProduct);//保存货物数据时需要考虑对购销合同的影响
        }
    }
}
